package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-11-16
 * Time: 10:52
 */
interface  IUSB{
    void openDevice();
    void closeDevice();
}
class  KeyBoard implements IUSB  {

    @Override
    public void openDevice() {
        System.out.println("打开键盘");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭键盘");
    }
    public void intput(){
        System.out.println("敲击键盘");
    }
}
class   Mouse implements IUSB{

    @Override
    public void openDevice() {
        System.out.println("打开鼠标");
    }

    @Override
    public void closeDevice() {
        System.out.println("关闭鼠标");
    }
    public void click(){
        System.out.println("点击鼠标");
    }
}
class Computer{
    public void open(){
        System.out.println("开机");
    }
    public void close(){
        System.out.println("关机");
    }
    public void useDevice(IUSB  usb){
        usb.openDevice();
        if(usb instanceof Mouse){
            Mouse mouse=(Mouse)usb;//向下转型
            mouse.click();//执行子类中特有的方法
        }else if(usb instanceof KeyBoard){
            KeyBoard keyBoard=(KeyBoard)usb;
            keyBoard.intput();
        }
        usb.closeDevice();
    }
}


public class Test {
    public static void main(String[] args) {
            Computer computer=new Computer();
                IUSB  iusb=new KeyBoard();//向上转型
                computer.useDevice(iusb);
                IUSB iusb1=new Mouse();
                computer.useDevice(iusb1);
    }

}
